package com.itheima.mm.controller;
import com.itheima.mm.base.BaseController;
import com.itheima.mm.common.GlobalConst;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.ReviewLog;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.ReviewLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：seanyang
 * @date ：Created in 2019/8/16
 * @description ：题目审核控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("/review")
@Slf4j
public class ReviewLogController extends BaseController {

	@Autowired
	private ReviewLogService reviewLogService;

	/**
	 * 增加审核
	 */
	@PostMapping("/add")
	public Result addReview (HttpServletRequest request,@RequestBody ReviewLog reviewLog)  {
		try{
			log.info("addReview reviewLog:{}",reviewLog);
			// 获取用户信息
			User user = getSessionUser(request, GlobalConst.SESSION_KEY_USER);
			// 从上下文获取用户ID，调试默认为1
			reviewLog.setUserId(user!=null?user.getId():1);
			reviewLogService.addReviewLog(reviewLog);
			return new Result(true,"操作成功");
		}catch(	RuntimeException e){
			log.error("addReview",e);
			return  new Result(false,"操作失败:"+e.getMessage());
		}
	}
}
